package cn.com.aiouyi.controller.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import cn.com.aiouyi.entity.Article;
import cn.com.aiouyi.entity.Photo;
import cn.com.aiouyi.entity.PhotoPosition;
import cn.com.aiouyi.page.Page;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.ArticleService;
import cn.com.aiouyi.service.PhotoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.aiouyi.entity.Navigation;
import cn.com.aiouyi.entity.NavigationPosition;
import cn.com.aiouyi.page.Filter;
import cn.com.aiouyi.page.Order;
import cn.com.aiouyi.service.LinkService;
import cn.com.aiouyi.service.NavigationPositionService;
import cn.com.aiouyi.service.NavigationService;

@Controller(value = "indexController")
@RequestMapping("/index")
public class IndexController extends BaseController{

	@Resource(name = "navigationServiceImpl")
	private NavigationService navigationService;
	@Resource(name = "navigationPositionServiceImpl")
	private NavigationPositionService navigationPositionService;

	@Resource(name = "photoServiceImpl")
	private PhotoService photoService;
	@Resource(name = "linkServiceImpl")
	private LinkService linkService;

	@Resource(name = "articleServiceImpl")
    private ArticleService articleService;

	@GetMapping
	public String index(ModelMap model){
		NavigationPosition navigationPositionCenter = navigationPositionService.findByCode(NavigationPosition.Code.indexCenter);
		if(navigationPositionCenter == null){
			model.put(ERROR_VIEW_MESSAGE_KEY, "中部导航位不存在");
			return ERROR_VIEW;
		}
		
		// 拿出中部导航位的导航
		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("isDisplay", true));
		filters.add(Filter.isNull("parent"));
		filters.add(Filter.eq("navigationPosition.id", navigationPositionCenter.getId()));
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.asc("seq"));
		List<Navigation> navigationCenters = navigationService.findList(null, filters, orders);
		Navigation navigationCenter1 = null, navigationCenter2 = null, navigationCenter3 = null;
		if(navigationCenters.size() > 1){
			navigationCenter1 = navigationCenters.get(0);
			navigationCenter1.getTemp().put("article", linkService.extractArticle(navigationCenter1.getLink()));
		}
		if(navigationCenters.size() > 2){
			navigationCenter2 = navigationCenters.get(1);
			navigationCenter2.getTemp().put("article", linkService.extractArticle(navigationCenter2.getLink()));
		}
		if(navigationCenters.size() > 3){
			navigationCenter3 = navigationCenters.get(2);
		}
		
		model.put("navigationCenter1", navigationCenter1);
		model.put("navigationCenter2", navigationCenter2);
		model.put("navigationCenter3", navigationCenter3);
		
		return "/web/index";
	}
	
	@GetMapping("/nav")
	public String navigation(ModelMap model){
		NavigationPosition navigationPosition = navigationPositionService.findByCode(NavigationPosition.Code.top);
		if(navigationPosition == null){
			throw new RuntimeException("顶部导航位不存在");
		}
		List<Filter> filters = new ArrayList<>();
		filters.add(Filter.eq("isDisplay", true));
		filters.add(Filter.isNull("parent"));
		filters.add(Filter.eq("navigationPosition.id", navigationPosition.getId()));
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.asc("seq"));
		model.put("navigations", navigationService.findList(null, filters, orders));
		return "/web/include/nav";
	}



	@GetMapping("/indexRotationPic")
	public String indexRotationPic(Pageable pageable, Model model){
		List<Filter> filters = pageable.getFilters();
		filters.add(Filter.eq("photoPosition.code", PhotoPosition.Code.indexBigImg));
		Page<Photo> photos = photoService.findPage(pageable);
		model.addAttribute("photos", photos.getContent());
		return "/web/include/indexRotationPic";
	}

	@GetMapping("/indexBottomPic")
	public String indexBottomPic(Pageable pageable, Model model){
		List<Filter> filters = pageable.getFilters();
		filters.add(Filter.eq("photoPosition.code", PhotoPosition.Code.indexBottomPic));
		Page<Photo> photos = photoService.findPage(pageable);
		model.addAttribute("photos", photos.getContent());
		return "/web/include/indexBottomPic";
	}

	@GetMapping("/indexLeftBottomPic")
	public String indexLeftBottomPic(Pageable pageable, Model model){
		List<Filter> filters = pageable.getFilters();
		filters.add(Filter.eq("photoPosition.code", PhotoPosition.Code.indexLeftBottomPic));
		Page<Photo> photos = photoService.findPage(pageable);
		model.addAttribute("photos", photos.getContent());
		return "/web/include/indexLeftBottomPic";
	}

	@GetMapping("/latestArticles")
	public String latestArticles(Pageable pageable, Model model){
		int pageSize = 7;
		List<Order> orders = pageable.getOrders();
		List<Filter> filters = pageable.getFilters();
        filters.add(Filter.eq("display", true));
        filters.add(Filter.eq("status", 0));
		orders.add(Order.desc("createDate"));
		pageable.setPageSize(pageSize);
		Page<Article> page = articleService.findPage(pageable);
		model.addAttribute("articles", page.getContent());
		return "/web/include/latestArticles";
	}


}
